package com.shj.em.biz.report.controller;

import com.alibaba.druid.util.StringUtils;
import com.shj.em.base.controller.BaseController;
import com.shj.em.base.support.ResMsg;
import com.shj.em.biz.prod.service.EffortService;
import com.shj.em.biz.report.dto.*;
import com.shj.em.biz.report.service.ReportService;
import com.shj.em.biz.hr.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {
    String prefix = "manage/report";

    @Autowired
    EmpService empService;

    @Autowired
    EffortService effortService;

    @Autowired
    ReportService reportService;

    @RequestMapping("/gotoSingleRate")
    public String gotoSingleRate(Model model){
        Calendar cal = Calendar.getInstance();
        addSingleRate(model, cal.get(Calendar.YEAR));
        return prefix + "/singleRate";
    }

    private void addSingleRate(Model model, int year){
        model.addAttribute("year", year);
        List<EmpEffortsDTO> empEffortsDTOList = reportService.getEmpEffortsStat(year);
        model.addAttribute("empEffortsDTOList", empEffortsDTOList);

    }

    @RequestMapping("/singleRate")
    public String searchSingleRate(@RequestParam("year") int year ,Model model){
        addSingleRate(model, year);
        return prefix + "/singleRate";
    }

    @RequestMapping("/gotoGroupRate")
    public String gotoGroupRate(Model model){
        Calendar cal = Calendar.getInstance();
        addGroupRate(model, cal.get(Calendar.YEAR));
        return prefix + "/groupRate";
    }

    private void addGroupRate(Model model, int year){
        model.addAttribute("year", year);
        List<GroupEffortDTO> groupEffortDTOList = reportService.getGroupEffortsStat(year);
        model.addAttribute("groupEffortDTOList", groupEffortDTOList);

    }

    @RequestMapping("/groupRate")
    public String searchGroupRate(@RequestParam("year") int year, Model model){
        addGroupRate(model, year);
        return prefix + "/groupRate";
    }

    @RequestMapping("/gotoGrowRatio")
    public String genReport(Model model){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);

        String error = reportService.validateConfigForGrowRatioReport(year);
        if(StringUtils.isEmpty(error)){
            addGrowRatioInfo(model, cal.get(Calendar.YEAR));

        }else{
            model.addAttribute("errorMsg", error);
        }
        return prefix + "/growRatio";
    }

    private void addGrowRatioInfo(Model model, int year){
        model.addAttribute("year", year);
        model.addAttribute("lastYear", year-1);
        GrowRatioDTO growRatio = reportService.getGrowRatio(year);
        List<Integer> target = growRatio.getCurrentYearTarget();
        int total = 0;
        for(Integer t : target){
            total += t;
        }
        model.addAttribute("totalTarget", total);
        model.addAttribute("growRatio", growRatio);
    }

    @RequestMapping("/growRatio")
    public String searchGrowRatio(@RequestParam("year") int year, Model model){
        addGrowRatioInfo(model, year);
        return prefix + "/growRatio";
    }

    @RequestMapping("/gotoReturnRatio")
    public String genReturnRatioReport(Model model){
        Calendar cal = Calendar.getInstance();
        addReturnRatioInfo(model, cal.get(Calendar.YEAR));
        return prefix + "/returnRatio";
    }

    private void addReturnRatioInfo(Model model, int year){
        model.addAttribute("year", year);
        ReturnRatioDTO returnRatio = reportService.genReturnRatioDTO(year);
        model.addAttribute("returnRatio", returnRatio);
    }

    @RequestMapping("/returnRatio")
    public String searchReturnRatio(@RequestParam("year") int year, Model model){
        addReturnRatioInfo(model, year);
        return prefix + "/returnRatio";
    }

    @RequestMapping("/gotoFirstGroupEffort")
    public String gotoFirstGroupEffort(Model model){
        getDayWeekMonth(model);
        return prefix + "/firstGroupEffort";
    }

    @GetMapping("/getFirstGroupDayEffort")
    @ResponseBody
    public ResMsg<FirstGrpEffortDTO> getFirstGroupDayEffort(@RequestParam("today") Date today){
        return ResMsg.ok(reportService.getFirstGroupDayEffort(today));
    }

    @GetMapping("/getFirstGroupWeekEffort")
    @ResponseBody
    public ResMsg<FirstGrpEffortDTO> getFirstGroupWeekEffort(@RequestParam("weekFrom") Date weekFrom,
                                                             @RequestParam("weekTo") Date weekTo){
        return ResMsg.ok(reportService.getFirstGroupWeekEffort(weekFrom, weekTo));
    }

    @GetMapping("/getFirstGroupMonthEffort")
    @ResponseBody
    public ResMsg<FirstGrpEffortDTO> getFirstGroupMonthEffort(@RequestParam("month") int month){
        return ResMsg.ok(reportService.getFirstGroupMonthEffort(month));
    }

    private void getDayWeekMonth(Model model){
        Calendar cal = Calendar.getInstance();

        // 日效率和周效率
        cal.add(Calendar.DAY_OF_MONTH, -1);
        model.addAttribute("today", cal.getTime());

        cal.add(Calendar.DAY_OF_MONTH, -7); // 一周前
        model.addAttribute("weekBefore", cal.getTime());

        // 月效率使用
        model.addAttribute("month", cal.get(Calendar.MONTH) + 1);
        List<Integer> months = new ArrayList<>();
        for(int i = 1; i <= 12; i++){
            months.add(i);
        }
        model.addAttribute("months", months);
    }

    @RequestMapping("/gotoDeviceUsage")
    public String deviceUsage(Model model){
        getDayWeekMonth(model);
        return prefix + "/deviceUsage";
    }

    @GetMapping("/getDayDeviceUsage")
    @ResponseBody
    public ResMsg<DeviceUsageDTO> getDayDeviceUsage(@RequestParam("today") Date today){
        return ResMsg.ok(reportService.getDayDeviceUsage(today));
    }

    @GetMapping("/getWeekDeviceUsage")
    @ResponseBody
    public ResMsg<DeviceUsageDTO> getWeekDeviceUsage(@RequestParam("weekFrom") Date weekFrom,
                                                     @RequestParam("weekTo") Date weekTo){
        return ResMsg.ok(reportService.getWeekDeviceUsage(weekFrom, weekTo));
    }

    @GetMapping("/getMonthDeviceUsage")
    @ResponseBody
    public ResMsg<DeviceUsageDTO> getMonthDeviceUsage(@RequestParam("month") int month){
        return ResMsg.ok(reportService.getMonthDeviceUsage(month));
    }

}
